package com.frkj.yuan.activity;

import android.content.SharedPreferences;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatRatingBar;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.frkj.yuan.R;
import com.frkj.yuan.adapter.BaseRecyclerHolder;
import com.frkj.yuan.adapter.BaseRecyclerRefreshAdapter;
import com.frkj.yuan.api.bean.DianpingBean;
import com.frkj.yuan.api.category.MyDianpingCategory;
import com.frkj.yuan.api.presenter.MyDianpingPresenter;
import com.frkj.yuan.base.BaseActivity;
import com.frkj.yuan.main.ConstantStaticUrl;
import com.frkj.yuan.utils.FullScreenUtils;
import com.frkj.yuan.utils.SPUtils;
import com.frkj.yuan.utils.ToastUtil;
import com.frkj.yuan.view.RecyclerViewLayoutManager;

import java.io.IOException;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.ycbjie.ycstatusbarlib.bar.YCAppBar;

/**
 * 信息说明: 我的点评界面
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/3/4 14 ：58
 * 项目名称：yuanban
 */
public class MyDianPingActivity extends BaseActivity implements MyDianpingCategory.View {
    @BindView(R.id.base_title)
    TextView mTitleMesg;
    @BindView(R.id.relat_empty)
    RelativeLayout mRealayout;
    @BindView(R.id.order_msg)
    TextView mOrderMsg;
    @BindView(R.id.base_recycler)
    RecyclerView mRecyclerViews;
    private String mToken;
    private long mUserId;
    private MyDianpingPresenter mPresenete;
    private BaseRecyclerRefreshAdapter<DianpingBean.DianpingBeanData.DianpingBeanDataList> mAdapter;
    private List<DianpingBean.DianpingBeanData.DianpingBeanDataList.DianpingMedias> imagelist;
    @Override
    public int getLayoutId() {
        return R.layout.activity_dianping_view;
    }
    @Override
    public void initLayoutView() {
        FullScreenUtils.getInstance().fullScreen(MyDianPingActivity.this, true);
        mTitleMesg.setText("我的点评");
        mOrderMsg.setVisibility(View.GONE);
        mToken = (String) SPUtils.get(MyDianPingActivity.this, ConstantStaticUrl.mToken, "");
        SharedPreferences sharedPreferences = MyDianPingActivity.this.getSharedPreferences("First", MODE_PRIVATE);
        mUserId = sharedPreferences.getLong(ConstantStaticUrl.userId, 0);
        RecyclerViewLayoutManager.setLinearVerticalManager(MyDianPingActivity.this, mRecyclerViews);
        mPresenete = new MyDianpingPresenter(this);
        SetPersenter(mPresenete);
        mPresenete.getDianping(mToken, mUserId);
    }

    @OnClick(R.id.base_back)
    public void OnClick() {
        finishWitchAnimation();
    }

    @Override
    public void getDianping(DianpingBean bean) {
        if (bean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            if (bean.getData() != null && bean.getData().getList().size() > 0) {
                mAdapter = new BaseRecyclerRefreshAdapter<DianpingBean.DianpingBeanData.DianpingBeanDataList>(MyDianPingActivity.this, bean.getData().getList(), R.layout.dian_ping_item) {
                    @Override
                    public void convert(BaseRecyclerHolder holder, DianpingBean.DianpingBeanData.DianpingBeanDataList item, int position, boolean isScrolling) throws IOException {
                        holder.setText(R.id.ping_msg, item.getContent());
                        holder.setText(R.id.dian_date, "点评日期 : " + item.getCommentTime());
                        holder.setText(R.id.service_pingfen, item.getScore() + "");
                        AppCompatRatingBar compatRatingBar = holder.getView(R.id.raging_ping);
                        compatRatingBar.setRating(item.getScore());
                        if (item.getPzOrderDTO() != null) {
                            holder.setText(R.id.serive_yiyuan,item.getPzOrderDTO().getHospitalName());
                            holder.setText(R.id.peizhen_name, item.getPzOrderDTO().getServiceName());
                            holder.setText(R.id.jiuzhen_ren, "就诊人 : " + item.getPzOrderDTO().getPatientName());
                            holder.setText(R.id.jiuzhen_keshi, "科室 ：" + item.getPzOrderDTO().getOfficeName());
                       holder.setText(R.id.serive_time,item.getPzOrderDTO().getServiceDay()+" "+item.getPzOrderDTO().getPeriodStr());
                        }
                        RecyclerView recyclerView = holder.getView(R.id.recycl_image);
                        RecyclerViewLayoutManager.setGridLayoutManager(MyDianPingActivity.this, recyclerView, 3);
                        imagelist = bean.getData().getList().get(position).getMedias();
                        recyclerView.setAdapter(new BaseRecyclerRefreshAdapter<DianpingBean.DianpingBeanData.DianpingBeanDataList.DianpingMedias>(MyDianPingActivity.this, imagelist, R.layout.icon_image_view) {
                            @Override
                            public void convert(BaseRecyclerHolder holder, DianpingBean.DianpingBeanData.DianpingBeanDataList.DianpingMedias item, int position, boolean isScrolling) throws IOException {
                                holder.setImageByUrl(R.id.image_comment, item.getUrl());
                            }
                        });
                    }
                };
                mRecyclerViews.setAdapter(mAdapter);
                mRealayout.setVisibility(View.GONE);
                mRecyclerViews.setVisibility(View.VISIBLE);
            }else {
                mRealayout.setVisibility(View.VISIBLE);
                mRecyclerViews.setVisibility(View.GONE);
            }
        }
    }
    @Override
    public void SetPersenter(Object persenter) {
    }

    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        ToastUtil.showToastERROR(MyDianPingActivity.this, mErrorMsg);
    }
}
